﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Aribaccio;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;

namespace BDPAutodetect.Models
{
    /// <summary>
    /// 系统参数配置视图模型数据
    /// </summary>
    public partial class MSettingSystem : MBase
    {
        /// <summary>
        /// 数据源改变事件
        /// </summary>
        public Action? DatumChangedAction;

        /// <summary>
        /// 名称类型
        /// </summary>
        [ObservableProperty]
        protected SettingType type;

        /// <summary>
        /// 数据源
        /// </summary>
        protected object? datum;
        /// <summary>
        /// 数据源
        /// </summary>
        public object? Datum
        {
            get => datum;
            set
            {
                if (SetProperty(ref datum, value))
                {
                    DatumChangedAction?.Invoke();
                }
            }
        }

        /// <summary>
        /// 数据类型枚举值
        /// </summary>
        [ObservableProperty]
        protected DataType dataType;

        /// <summary>
        /// 构造函数
        /// </summary>
        public MSettingSystem() : base()
        {
            this.Name = SettingType.None.ToStringName();
            this.Type = SettingType.None;
            this.DataType = DataType.String;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public MSettingSystem(int argID, string argName) : this()
        {
            this.ID = argID;
            if (!string.IsNullOrEmpty(argName))
            {
                this.Name = argName;
                this.Type = this.Name.ToEnum<SettingType>();
            }
        }

        /// <summary>
        /// 克隆表数据
        /// </summary>
        /// <returns></returns>
        public TBasicSystems CloneTData()
        {
            return new TBasicSystems()
            {
                ID = this.ID,
                Name = this.Name,
                Datum = this.Datum?.ToString(),
                DataType = this.DataType.ToInt()
            };
        }

        /// <summary>
        /// 获取系统参数数据
        /// </summary>
        /// <typeparam name="TData">数据值类型</typeparam>
        /// <param name="argType">系统参数枚举类型</param>
        /// <param name="argDefault">默认数据值</param>
        /// <returns></returns>
        public TData GetData<TData>(TData argDefault) where TData : struct
        {
            if (this.Datum == null || string.IsNullOrEmpty(this.Datum.ToString())) return argDefault;
            var valType = typeof(TData);
            var dataType = this.ConvertDataType();
            return (valType == dataType)
                  ? (TData)Convert.ChangeType(this.Datum, valType)
                  : argDefault;
        }

        /// <summary>
        /// 获取数据的系统类型
        /// </summary>
        /// <returns></returns>
        protected Type ConvertDataType()
        {
            switch (this.DataType)
            {
                case DataType.Decimal:
                    return typeof(decimal);
                case DataType.Int:
                    return typeof(int);
                case DataType.Bool:
                    return typeof(bool);
                case DataType.String:
                    return typeof(string);
                default:
                    return null;
            }
        }
    }
}
